﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;  
using System.Text;
using System.Collections.Specialized ;
using System.Web.Routing;
using System.Linq.Expressions;
namespace System.Web.Helpers
{
    public enum FormType
    {
        Horizontal,
        Vertical
    }
    public enum InputSize
    {
        input_mini,
        input_small,
        input_large,
        input_xlarge,
        input_xxlarge

    }
    public enum ButtonType
    {
        Normal,
        submit,
        reset
    
    }
        public enum enumRowType
        {
               Normal,
            success,
            error,	
            warning,	
            info
        }

        public enum enmIcon
        {
            adjust,
            align_center,
            align_justify,
            align_left,
            align_right,
            arrow_down,
            arrow_left,
            arrow_right,
            arrow_up,
            asterisk,
            backward,
            ban_circle,
            barcode,
            bell,
            bold,
            book,
            bookmark,
            briefcase,
            bullhorn,
            calendar,
            camera,
            certificate,
            check,
            chevron_down,
            chevron_left,
            chevron_right,
            chevron_up,
            circle_arrow_down,
            circle_arrow_left,
            circle_arrow_right,
            circle_arrow_up,
            cog,
            comment,
            download,
            download_alt,
            edit,
            eject,
            envelope,
            exclamation_sign,
            eye_close,
            eye_open,
            facetime_video,
            fast_backward,
            fast_forward,
            file,
            film,
            filter,
            fire,
            flag,
            folder_close,
            folder_open,
            font,
            forward,
            fullscreen,
            gift,
            glass,
            globe,
            hand_down,
            hand_left,
            hand_right,
            hand_up,
            hdd,
            headphones,
            heart,
            home,
            inbox,
            indent_left,
            indent_right,
            info_sign,
            italic,
            leaf,
            list,
            list_alt,
            _lock,
            magnet,
            map_marker,
            minus,
            minus_sign,
            move,
            music,
            off,
            ok,
            ok_circle,
            ok_sign,
            pause,
            pencil,
            picture,
            plane,
            play,
            play_circle,
            plus,
            plus_sign,
            print,
            qrcode,
            question_sign,
            random,
            refresh,
            remove,
            remove_circle,
            remove_sign,
            repeat,
            resize_full,
            resize_horizontal,
            resize_small,
            resize_vertical,
            retweet,
            road,
            screenshot,
            search,
            share,
            share_alt,
            shopping_cart,
            signal,
            star,
            star_empty,
            step_backward,
            step_forward,
            stop,
            tag,
            tags,
            tasks,
            text_height,
            text_width,
            th,
            th_large,
            th_list,
            thumbs_down,
            thumbs_up,
            time,
            tint,
            trash,
            upload,
            user,
            volume_down,
            volume_off,
            volume_up,
            warning_sign,
            wrench,
            zoom_in,
            zoom_out
         
    

        }

    public static class MyBootStrapHelper
    {
        private static String ReplaceUnderScore(string ss)
        {
            if (ss.IndexOf("_") == 0) ss = ss.Substring(1);  
            return ss.Replace("_", "-").Trim();
        }

        private static void RegisterControlInitScript(string sScript, ref HtmlHelper helper)
        {
            StringBuilder sb;
            if (helper.ViewBag.ControlScript == null)
            sb = new StringBuilder();
            else
            sb = helper.ViewBag.ControlScript as StringBuilder;
                        sb.AppendFormat("{0}\n", sScript.Trim());
                        helper.ViewBag.ControlScript = sb;
       }

              public static MvcHtmlString BsIcon(this HtmlHelper helper, enmIcon icon)
        {
            return new MvcHtmlString(string.Format("<i class='icon-{0}'></i>", ReplaceUnderScore(icon.ToString())));
        }
		private static MvcHtmlString RenderControlScript(HtmlHelper helper)
        {
            if (helper.ViewBag.ControlScript != null)
            {
                StringBuilder sb = helper.ViewBag.ControlScript as StringBuilder;
                return new MvcHtmlString(sb.ToString());

            }
            else
                return new MvcHtmlString("");
 		}


       
        
 
        public static MvcHtmlString BsTextInputWithLable(this HtmlHelper helper, string Name,object Value, string sLable, object htmlAttributes,string sFormat, InputSize size )
        {
            return BsTextInputWithLableInternal(helper, Name, Value, sLable, htmlAttributes, sFormat, size);   
        }
        public static MvcHtmlString BsTextInputWithLable(this HtmlHelper helper, string Name, string sLable, InputSize size)
        {
            return BsTextInputWithLableInternal(helper, Name, "",sLable, null, "", size);
        }
        //public static MvcHtmlString BsTextInputWithLable<TModel, TProp>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProp>> expression, string sLable, InputSize size)
        //{
        //    return 
        //}
        private static MvcHtmlString BsTextInputWithLableInternal(this HtmlHelper helper, string Name,object Value, string sLable, object htmlAttributes,string sFormat, InputSize size )
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='control-group'>");
            sb.AppendFormat("<label class='control-label' for='{0}'>{1}</label>", Name, sLable);
            sb.Append("<div class='controls'>");
            string CssClass=ReplaceUnderScore(size.ToString());
            RouteValueDictionary ha;
            if(htmlAttributes!=null) 
                ha=new RouteValueDictionary(htmlAttributes);
            else
                ha=new RouteValueDictionary();
            if (!ha.ContainsKey("class"))
                ha.Add("class",CssClass); 
            
            sb.Append(helper.TextBox(Name,Value,sFormat,ha).ToHtmlString()); 
            //sb.AppendFormat(" <input type='text' class='{0}' id='{1}'>", ReplaceUnderScore(size.ToString()), Name);
            sb.Append("</div></div>");
            return new MvcHtmlString(sb.ToString());
        }

        public static BSFormBuilder BSForm(this HtmlHelper helper, FormType _forrmtype, string FormTital)
        {
            string formtype = _forrmtype == FormType.Horizontal ? "form-horizontal" : "form-vertical";
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<form class='{0}'method='post' action='{1}'>", formtype,new UrlHelper(helper.ViewContext.RequestContext).Action(null));
            sb.AppendFormat("<fieldset><legend>{0}</legend>", FormTital);
            helper.ViewContext.Writer.WriteLine(sb.ToString());

            return new BSFormBuilder(helper.ViewContext);

        }

        public static BSFormButtonGroup BSFormButtons(this HtmlHelper helper)
        {
            helper.ViewContext.Writer.WriteLine("<div class='form-actions'>");
            return new BSFormButtonGroup(helper.ViewContext);
        }

        public static MvcHtmlString RenderScript(this HtmlHelper helper)
        {
            StringBuilder sb = new StringBuilder();
            Dictionary<string, string> sCollection = helper.ViewBag.ScriptCollection;
            
            return new MvcHtmlString(sb.ToString());
 
        }
        public static MvcHtmlString BSButton(this HtmlHelper helper, string ID, string sValue, string Action, bool IsPrimery, ButtonType btype)
        {
            helper.ViewBag.ScriptCollection = new Dictionary<string,string>();


            string btnClass = "btn";
            StringBuilder tag = new StringBuilder();

            if (IsPrimery)
                btnClass += " btn-primary";
            tag.AppendFormat(" <button class='{0}' id='{1}' name='{1}'", btnClass, ID);
            if (Action != "")
                tag.AppendFormat(" onclick='{0}' ", Action);
            switch (btype)
            {

                case ButtonType.Normal:
                    tag.Append(">");
                    break;
                case ButtonType.reset:
                    tag.AppendFormat(" type='reset'>");
                    break;
                case ButtonType.submit:
                    tag.AppendFormat(" type='submit'>");
                    break;
                default:
                    tag.Append(">");
                    break;

            }

            tag.AppendFormat("{0}</button> ", sValue);
            return new MvcHtmlString(tag.ToString());
        }

        public static BSTable   BSHtmlTable(this HtmlHelper helper, bool IsStriped, bool IsBorder,bool IsHover)
        {
            StringBuilder sb=new StringBuilder();
            sb.AppendFormat("<table class='table table-condensed{0}{1}{2}'>", (IsStriped ? " table-striped" : ""), (IsBorder ? " table-bordered" : ""), (IsHover ? " table-hover" : ""));//bordered
            helper.ViewContext.Writer.Write(sb.ToString());

            return new BSTable(helper.ViewContext); 
        }

        public static MvcHtmlString BSTableRow(this HtmlHelper helper, enumRowType RowType, params string[] RowItems)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat ("<tr{0}>",(RowType!=enumRowType.Normal?" class='"+RowType.ToString()+"'":""));
            foreach (string ss in RowItems)
                sb.AppendFormat("<td>{0}</td>", ss);
            sb.Append("</tr>");
            return new MvcHtmlString(sb.ToString()); 
        }

        public static BsTabContainer BeGinBsTabContaint(this HtmlHelper helper, string ContainerId, bool IsActive)
        {
            return new BsTabContainer(helper.ViewContext, ContainerId, IsActive); 
        }
        public static BsTabPan BeginBSTabPan(this HtmlHelper helper)
        {
          
            StringBuilder sb = new StringBuilder();
            sb.Append ("<div class='tabbable'>");
            sb.Append("<ul class='nav nav-tabs'>");
            BsTabPan  t =new BsTabPan(helper.ViewContext);
             helper.ViewContext.Writer.Write(sb.ToString());

            return t;
        }
    }



    #region BSTable

    

    public class BSTable : IDisposable
    {
        private ViewContext _ViewContext;
        public BSTable(ViewContext vc)
        {
            this._ViewContext = vc;
        }
        public BSTable AddHeaders(params string[] headers)
        {
            StringBuilder sb=new StringBuilder();
            sb.Append("<thead><tr>");
            foreach(string ss in headers)
            sb.AppendFormat("<th>{0}</th>",ss); 
            sb.Append("</tr></thead>");
            _ViewContext.Writer.Write(sb.ToString());
            return this;
        }
        public void Dispose()
        {

            _ViewContext.Writer.Write("</table>");
        }
    }
    #endregion
    #region BSFormBuilder


    public class BSFormBuilder : IDisposable
    {
        public ViewContext myContext;
        public BSFormBuilder(ViewContext _context)
        {
            myContext = _context;
        }



        #region IDisposable Members

        public void Dispose()
        {
            myContext.Writer.WriteLine(" </fieldset></form>");
        }

        #endregion
    }
    #endregion

    #region BSControlGroupBuilder

    public class BSControlGroupBuilder : IDisposable
    {
        ViewContext viewContext;
        public BSControlGroupBuilder(ViewContext _viewContext)
        {
 
        }

        public void Dispose()
        {

 
        }
    }

    #endregion

   
    #region BSFormButtonGroup

    public class BSFormButtonGroup : IDisposable
    {
        private ViewContext viewcontext;
        public BSFormButtonGroup(ViewContext _viewContext)
        {
            viewcontext = _viewContext;
        }
        public void Dispose()
        {
            viewcontext.Writer.WriteLine("</div>");
        }
    }

    #endregion
}